﻿Imports DTO
Imports System.Data.SqlClient

Public Class ChiTietDAO

    Public Function LayTableCTHoaDonDAO(ByVal _maHD As Integer) As DataTable
        Dim sql As String = "sp_LayChiTietHoaDon"

        Dim _listPara As New List(Of SqlParameter)
        Dim MaHD As New SqlParameter("@MaHD", SqlDbType.Int)
        MaHD.Value = _maHD
        _listPara.Add(MaHD)

        Dim dt As New DataTable()
        dt = DataProvider.LoadSqlProcH(sql, _listPara)

        Return dt
    End Function
    Public Sub ThemCTHDDAO(ByVal _chiTietDTO As ChiTietDTO)

        Dim sql As String = "sp_ThemCTThucDon"
        Dim _listPara As New List(Of SqlParameter)

        Dim MaHD As New SqlParameter("@MaHD", SqlDbType.Int)
        MaHD.Value = _chiTietDTO.MaHD
        _listPara.Add(MaHD)

        Dim MaMenu As New SqlParameter("MaMenu", SqlDbType.Int)
        MaMenu.Value = _chiTietDTO.MaMenu
        _listPara.Add(MaMenu)

        Dim SoLuong As New SqlParameter("@Soluong", SqlDbType.Int)
        SoLuong.Value = _chiTietDTO.SoLuong
        _listPara.Add(SoLuong)

        DataProvider.ExcuteQueryProc(sql, _listPara)

    End Sub

    Public Function LoadDanhSachChiTietDAO(ByVal _maHD As Integer) As List(Of ChiTietDTO)

        Dim sql As String = "Select * from ChiTiet where isdel = 0 and MaHD = " & _maHD

        Dim dt As New DataTable()
        dt = DataProvider.LoadSQL(sql)

        '_iMaCT = New Integer()
        '_iMaHD = New Integer()
        '_iMaMenu = New Integer()
        '_iSoLuong = New Integer()
        '_dDonGia = New Double()
        '_dTongTien = New Double()
        Dim _listCTHD As New List(Of ChiTietDTO)
        For Each row As DataRow In dt.Rows
            Dim MaCT As Integer = Integer.Parse(row("MaCT").ToString())
            Dim MaHD As Integer = Integer.Parse(row("MaHD").ToString())
            Dim MaMenu As Integer = Integer.Parse(row("MaMenu").ToString())
            Dim SoLuong As Integer = Integer.Parse(row("SoLuong").ToString())
            Dim DonGia As Double = Double.Parse(row("DonGia").ToString())
            Dim TongTien As Double = Double.Parse(row("TongTien").ToString())
            Dim _ctDTO As New ChiTietDTO(MaCT, MaHD, MaMenu, SoLuong, DonGia, TongTien)
            _listCTHD.Add(_ctDTO)
        Next
        Return _listCTHD
    End Function

    Public Sub SuaCTHoaDonDAO(ByVal _maCT As Integer, ByVal _soLuong As Integer)

        Dim sql As String = "sp_SuaCTHoaDon"

        Dim _listPara As New List(Of SqlParameter)

        Dim MaCT As New SqlParameter("@MaCT", SqlDbType.Int)
        MaCT.Value = _maCT
        _listPara.Add(MaCT)

        Dim SoLuong As New SqlParameter("@SoLuong", SqlDbType.Int)
        SoLuong.Value = _soLuong
        _listPara.Add(SoLuong)

        DataProvider.ExcuteQueryProc(sql, _listPara)

    End Sub

    Public Sub XoaCTHoaDonDAO(ByVal _maCT As Integer)
        Dim sql As String = "sp_XoaCTHoaDon"

        Dim _listPara As New List(Of SqlParameter)

        Dim MaCT As New SqlParameter("@MaCT", SqlDbType.Int)
        MaCT.Value = _maCT
        _listPara.Add(MaCT)

        DataProvider.ExcuteQueryProc(sql, _listPara)
    End Sub
End Class
